// platform.ts

import { httpTauri } from "@/utils/http";
import { localServer } from "@/api/utils";
import { PageResult, Result } from "@/api/common";

export interface LabPlatformVO {
  id: string;
  name: string;
  desc: string;
  parentId: string;
  isDefault: boolean;
  sensors: PlatformDevice[]; // Replace 'any' with the actual type if known
  createUser: string;
  createTime: string;
  updateUser: string;
  updateTime: string;
  curUser: string;
  copyFrom: string;
}

export interface PlatformDevice {
  expId: string;
  name: string;
  deviceId: string;
  interval: number;
  ext: PlatformDeviceExt;
}

export interface PlatformDeviceExt {
  name: string;
  desc: string;
}

export const addPlatformDevice = async (data: object) => {
  return httpTauri.request<Result<PlatformDevice>>(
    "post",
    localServer("/api/experiment/platform/device/add"),
    {
      data
    }
  );
};

export const addPlatform = async (data: LabPlatformVO) => {
  return httpTauri.request<Result<LabPlatformVO>>(
    "post",
    localServer("/api/experiment/platform/addRoot"),
    {
      data
    }
  );
};

export const getPlatform = async expId => {
  return httpTauri.request<Result<LabPlatformVO>>(
    "get",
    localServer(`/api/experiment/platform/get?expId=${expId}`)
  );
};

export const copyPlatform = async (data: object) => {
  return httpTauri.request<Result<LabPlatformVO>>(
    "post",
    localServer("/api/experiment/platform/addConfig"),
    {
      data
    }
  );
};

export const listPlatforms = async () => {
  return httpTauri.request<Result<Array<LabPlatformVO>>>(
    "get",
    localServer("/api/experiment/platform/listAll?CurPage=1&pageSize=1000")
  );
};

export const listParentPlatform = async (params: object) => {
  return httpTauri.request<Result<Array<LabPlatformVO>>>(
    "get",
    localServer("/api/experiment/platform/list"),
    { params }
  );
};

export const setDefaultPlatform = async (expId: string) => {
  const data = { expId: expId, isDefault: true };
  return httpTauri.request<Result<Array<LabPlatformVO>>>(
    "post",
    localServer(`/api/experiment/platform/default`),
    { data }
  );
};

export const deletePlatform = async (expId: number) => {
  const data = { expId: expId };
  return httpTauri.request<Result<Array<LabPlatformVO>>>(
    "post",
    localServer(`/api/experiment/platform/delete`),
    { data }
  );
};
export const updatePlatform = async (data: object) => {
  return httpTauri.request<Result<Array<LabPlatformVO>>>(
    "post",
    localServer(`/api/experiment/platform/update`),
    { data }
  );
};

export const listPagePlatforms = async (
  pageSize: number,
  curPage: number
): Promise<PageResult<Array<LabPlatformVO>>> => {
  return httpTauri.request<PageResult<Array<LabPlatformVO>>>(
    "get",
    localServer(
      `/api/experiment/platform/listAll?pageSize=${pageSize}&curPage=${curPage}`
    )
  );
};
